import axios from "axios";
// import store from '@/store'
import {Loading} from "element-ui"; //引用element-ui的加载提示组件
let loading;
function startLoading() {
    loading = Loading.service({
        lock: true, text: "努力加载中……", background: "rgba(0, 0, 0, 0.1)"
    });
}
function endLoading() {
    loading.close();
}
// loading互不冲突调用
let needLoadingRequestCount = 0;

export function showFullScreenLoading() {
    if (needLoadingRequestCount === 0) {
        startLoading();
    }
    needLoadingRequestCount++;
}

export function tryHideFullScreenLoading() {
    if (needLoadingRequestCount <= 0) return;
    needLoadingRequestCount--;
    if (needLoadingRequestCount === 0) {
        endLoading();
    }
}

// 默认超时设置
/* eslint-disable */

axios.defaults.timeout = 6000000 * 10
// axios.defaults.timeout = 10400000 * 2
// 相对路径设置
axios.defaults.baseURL = ''
// loading图

// http request 拦截器
axios.interceptors.request.use(config => {
    // 设置参数格式
    const UserInfo=JSON.parse(localStorage.getItem('userinfo'))
    config.headers = {
        token:  window.btoa(UserInfo.userId + '|' +UserInfo.departId + '|' + '209780b2a6368c80e4b38a8d2586e9ea'),
        'stdapptkn':localStorage.getItem('token'),
        'Content-Type': 'application/json', 'Authorization': localStorage.getItem('token')
    }
    // 判断ie加时间戳防止不请求接口
    if (window.ActiveXObject || 'ActiveXObject' in window) {
        config.url = `${config.url}?time=${new Date().getTime()}`
    }
    showFullScreenLoading()
    return config
}, err => {
    return Promise.reject(err)
})
// http response 拦截器
axios.interceptors.response.use(response => {
    tryHideFullScreenLoading()
    return response
}, error => {
    tryHideFullScreenLoading()
    return Promise.reject(error)
})




/**
 * 封装get方法
 * @param url
 * @param params
 * @returns {Promise}
 */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        let startTime = new Date()
        return axios.get(url, {
            params: params
        }).then(response => {

            if (response.data.code == 0) {
                resolve(response.data.data)
            } else if (response.data.code == 500) {
                reject(response.data.msg)
            } else {
                reject(response.data.msg)

            }

            printLog(url, params, response.data || {}, startTime)
        }).catch(err => {
            reject(err)

        })
    })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        let startTime = new Date()
        axios.post(url, JSON.stringify(data)).then(response => {
            if (response.data.code == 0||response.data.code == 200) {
                resolve(response.data.data)
            }  else {
            
                this.$notify.error({
                    title: '错误',
                    message: response.data.msg
                });
                reject(response.data.msg)
            }
            printLog(url, data, response.data || {}, startTime)
        }).catch(err => {
        
            reject(err)

        })
    })
}

export function upload(url, data = {}) {
    return new Promise((resolve, reject) => {
        let startTime = new Date()
        axios.post(url,data,{
            headers: {
                'Content-Type': 'application/form-data',
            }
            }).then(response => {
            if (response.data.code == 0) {
                resolve(response.data.data)
            } else if (response.data.code == 500) {
                reject(response.data.msg)
            } else {
                reject(response.data.msg)
            }
            printLog(url, data, response.data || {}, startTime)
        }).catch(err => {
           
            reject(err)

        })
    })
}



/* eslint-disable */

/**
 *
 * @param url
 * @param params
 * @param data
 * @param startTime
 */
function printLog(url, params, data, startTime) {
    try {
      
    } catch (e) {

    }
}
